/**
 * Copyright 2011 HNB. All rights reserved.
 * HNB PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package my.hnb.android.view.dialog;

import java.util.ArrayList;

import my.hnb.android.dto.OptionItemDTO;
import my.hnb.android.listenner.OnEventListener;
import my.hnb.android.ultils.AppUltils;
import my.hnb.android.ultils.SpannableObject;
import my.hnb.android.view.base.BaseActivity;
import my.hnb.songnhac.R;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

/**
 * Man hinh danh sach option
 * @author BANGHN
 */
public class OptionDialogView extends LinearLayout implements OnItemClickListener, OnClickListener{
	BaseActivity mContext;
	public View dialogView = null;
	ListView lvOption;
	TextView tvTitle;
	ImageView ivIcon;
	Button btButton;
	ArrayList<OptionItemDTO> arrayOption = new ArrayList<OptionItemDTO>();
	OnEventListener listenner;
	int action = -1;
	public OptionDialogView(Context context) {
		super(context);
		mContext = (BaseActivity) context;
		initView();
	}

	
	public OptionDialogView(Context context, ArrayList<OptionItemDTO> arrayOption) {
		super(context);
		//this.arrayOption = (ArrayList<OptionItemDTO>) arrayOption.clone();
		for(int i = 0; i < arrayOption.size(); i++){
			this.arrayOption.add(new OptionItemDTO(arrayOption.get(i)));
		}
		mContext = (BaseActivity) context;
		listenner = (OnEventListener) context;
		initView();
		initDataOptionDialog();
	}

	
	public void setTileAndIcon(String title, Drawable icon){
		tvTitle.setText(title);
		ivIcon.setImageDrawable(icon);
	}
	
	/**
	 * Set du lieu option neu chua duoc khoi tao
	 * @author BANGHN
	 * @param arrayOption
	 */
	@SuppressWarnings("unchecked")
	public void setArrayOption(ArrayList<OptionItemDTO> arrayOption){
		this.arrayOption = (ArrayList<OptionItemDTO>) arrayOption.clone();
		initDataOptionDialog();
	}
	
	public void setAction(int action){
		this.action = action;
	}
	
	/**
	 * Khoi tao view layout
	 * @author BANGHN
	 */
	private void initView(){
		//LayoutInflater inflater = mContext.getLayoutInflater();
		LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		dialogView = inflater.inflate(R.layout.layout_dialog_list, this);
		lvOption = (ListView)dialogView.findViewById(R.id.lvOption);
		tvTitle = (TextView)dialogView.findViewById(R.id.tvTitle);
		ivIcon = (ImageView)dialogView.findViewById(R.id.ivIcon);
		btButton = (Button)dialogView.findViewById(R.id.btButton);
		lvOption.setOnItemClickListener(this);
		btButton.setOnClickListener(this);
	}
	
	
	@Override
	public void onClick(View v) {
		if(v == btButton){
			listenner.onEvent(null, action, this.arrayOption);
		}
		
	}
	
	/**
	 * Khoi tao du lieu dialog
	 * @author BANGHN
	 * @param arrayOption
	 */
	private void initDataOptionDialog(){
		OptionAdapter optionAdapter = new OptionAdapter(this.mContext, arrayOption);
		lvOption.setAdapter(optionAdapter);
	}
	
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		//Log.i("onItemClick", "......" + arg2);
	}
	
	/**
	 * adapter render danh muc option trong dialog
	 * @author BANGHN
	 */
	class OptionAdapter extends BaseAdapter implements OnClickListener{
		OnEventListener listener;
		private Context context;
		private ArrayList<OptionItemDTO> listOption;

		public OptionAdapter(Context ct, ArrayList<OptionItemDTO> listDTO) {
			listOption = listDTO;
			context = ct;
			this.listener = (OnEventListener) ct;
		}

		public void updateListSongs(ArrayList<OptionItemDTO> listDTO) {
			listOption = listDTO;
		}

		@Override
		public int getCount() {
			if (listOption != null)
				return listOption.size();

			return 0;
		}

		@Override
		public Object getItem(int position) {
			return listOption.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = new DialogOptionRow(context);
			}
			((DialogOptionRow) convertView).populateFrom((OptionItemDTO) getItem(position));
			((DialogOptionRow) convertView).cbCheck.setTag(position);
			((DialogOptionRow) convertView).cbCheck.setOnClickListener(this);
			if(position == 0){
				SpannableObject txtAll = new SpannableObject();
				txtAll.addSpan(((OptionItemDTO) getItem(position)).strText,
						AppUltils.getColor(R.color.BLACK_COLOR),
						android.graphics.Typeface.BOLD);
				((DialogOptionRow) convertView).tvText.setText(txtAll.getSpan());
			}
			
			return convertView;
		}
		
		@Override
		public void onClick(View v) {
			Integer pos = (Integer)v.getTag();
			if(v.getId() == R.id.cbCheck){
				listOption.get(pos).setIsSelected(!listOption.get(pos).isSelected());
				int size = listOption.size();
				//tat ca
				if(pos == 0){
					boolean value = listOption.get(pos).isSelected();
					for(int i = 1; i < size; i++){
						listOption.get(i).setIsSelected(value);
					}
					this.notifyDataSetChanged();
				}else{
					boolean isAll = true;
					for(int i = 1; i < size; i++){
						if(listOption.get(i).isSelected() != isAll){
							isAll = false;
							break;
						}
					}
					listOption.get(0).setIsSelected(isAll);
					this.notifyDataSetChanged();
				}
			}
		}

	}
}
